# autoutils.R
# my ordered list implementation

# exemplo:
# ord <- function(a,b) a - b
# order_add(c(1,4,6,9,20), 8, ord)
# [1]  1  4  6  8  9 20

insert_list <- function(lst, index, item)
{
	rtn <- vector('list', length(lst)+1)
	if(index > 1)
		for(i in 1:(index-1))
			rtn[[i]] <- lst[[i]]
	rtn[[index]] <- item
	for(i in index:length(lst))
		rtn[[i+1]] <- lst[[i]]
	rtn
}

order_add <- function(lst, item, ord_func)
{
	if(length(lst) > 1)
		for(i in 1:length(lst)) {
			v <- ord_func(lst[[i]], item)
			if(v >= 0)
				return(insert_list(lst, i, item))
		}
	lst[[length(lst)+1]] <- item
	lst
}

list_contains <- function(lst, item, cmp)
{
	for(i in lst)
		if(cmp(i, item))
			return(TRUE)
	FALSE
}

